package metaphor.persistence.filter;

import java.io.Serializable;

/**
 * A simple filter expression
 * @author Mark Ashworth
 * @version 1.0.0
 */
public class SimpleExpression implements Expression {
	
	private String fieldName;
	private Operator operator;
	private Serializable operand;

	public SimpleExpression() {
	}
	
	public SimpleExpression(String fieldName, Operator operator, Serializable operand) {
		this.fieldName = fieldName;
		this.operator = operator;
		this.operand = operand;
	}
	
	/**
	 * The following expresses that the price should be greater or equal to
	 * 100.<br />
	 * <i>SimpleFilter.ge("price", 100);</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression ge(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.GE, operand);
	}
	
	/**
	 * The following expresses that the price should be greater than
	 * 100.<br />
	 * <i>SimpleFilter.gt("price", 100);</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression gt(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.GT, operand);
	}
	
	/**
	 * The following expresses that the price should be less or equal to
	 * 100.<br />
	 * <i>SimpleFilter.le("price", 100);</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression le(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.LE, operand);
	}
	
	/**
	 * The following expresses that the price should be less than
	 * 100.<br />
	 * <i>SimpleFilter.lt("price", 100);</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression lt(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.LT, operand);
	}
	
	/**
	 * The following expresses that the price should be equal to
	 * 100.<br />
	 * <i>SimpleFilter.eq("price", 100);</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression eq(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.EQ, operand);
	}
	
	/**
	 * The following expresses that the name should be like '%JO%'.<br />
	 * <i>SimpleFilter.like("name", "%JO%");</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression like(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.LIKE, operand);
	}

    /**
	 * The following expresses that the name should be like '%JO%'.<br />
	 * <i>SimpleFilter.like("name", "%JO%");</i>
	 * 
	 * @param fieldName The name of the field
	 * @param operand The operand value
	 * @return SimpleFilter
	 */
	public static SimpleExpression ilike(String fieldName, Serializable operand) {
		return new SimpleExpression(fieldName, Operator.ILIKE, operand);
	}
    
    public String getFieldName() {
        return fieldName;
    }

    public Serializable getOperand() {
        return operand;
    }

    public Operator getOperator() {
        return operator;
    }

//	protected String getOperandExpression(FilterLanguage filterLanguage, Serializable value) {
//		final String APOSTROPHE = filterLanguage.symbol(Symbol.APOSTROPHE);
//		
//		if (value == null) {
//			return "null";
//		}
//		if (value instanceof Number) {
//			return value.toString();
//		} else if (value instanceof String) {
//			return APOSTROPHE + (String) value + APOSTROPHE;
//		} else if (value instanceof Date) {
//			return APOSTROPHE + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format((Date) value) + APOSTROPHE;
//		} else {
//			return APOSTROPHE + value.toString() + APOSTROPHE;
//		}
//	}
//	
//	public String getExpression(FilterLanguage filterLanguage) {
//		final String SPACE = " ";
//		return 
//			fieldName + SPACE
//			+ filterLanguage.operator(operator) + SPACE
//			+ getOperandExpression(filterLanguage, operand);
//	}
}
